package com.zt.dev.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.zt.dev.common.ResponseUtils;
import com.zt.dev.common.ServerResponse;
import com.zt.dev.system.dto.TransformBeans;
import com.zt.dev.system.pojo.SystemRelation;
import com.zt.dev.system.service.SystemRelationService;
import com.zt.dev.system.vo.ReceiveRelationVo;
import com.zt.dev.system.vo.SystemStructureVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 30378
 * SystemRelation Relation 是各个指标组成的结构关系
 */
@Controller
public class SystemRelationController {

    private final SystemRelationService relationService;
    private final TransformBeans transformBeans;


    public SystemRelationController(SystemRelationService relationService, TransformBeans transformBeans) {
        this.relationService = relationService;
        this.transformBeans = transformBeans;
    }

    /**
     * 新增指标关联关系
     */
    @PostMapping("/addSysRelation")
    @ResponseBody
    public ServerResponse addSysRelation(@RequestBody JSONArray structureList) {
        if (structureList.size() > 0) {
            SystemStructureVo sys = transformBeans.getSysStructureRelation(structureList);
            int i = relationService.addSysRelation(sys, structureList);
            return ResponseUtils.commonResponse(i, "新增指标关联关系");
        }
        return ServerResponse.createByErrorMessage("添加的指标为空");
    }

    /**
     * 根据id删除指标
     *
     * @param id 体系ID
     */
    @PostMapping("/delSysRelation")
    @ResponseBody
    public ServerResponse delSysRelation(Integer id) {
        if (id != null) {
            int i = relationService.delSysRelation(id);
            return ResponseUtils.commonResponse(i, "删除指标关联关系");
        } else {
            return ServerResponse.createByErrorMessage("删除的指标为空");
        }
    }

    /**
     * 更新体系关系
     *
     * @param relation 要更新的体系内容
     */
    @PostMapping("/updateSysRelation")
    @ResponseBody
    public ServerResponse updateSysRelation(ReceiveRelationVo relation) {
        if (relation != null && relation.getId() != null) {
            int i = relationService.updateSysRelation(relation);
            return ResponseUtils.commonResponse(i, "更新指标关联关系");
        } else {
            return ServerResponse.createByErrorMessage("更新指标失败，参数错误");
        }

    }

    /**
     * 查找指标
     **/
    @PostMapping("/findSysRelationById")
    @ResponseBody
    public ServerResponse findSysRelationById(Integer id) {
        if (id != null) {
            SystemRelation relation = relationService.findSysRelationById(id);
            return ServerResponse.createBySuccess(relation);
        }
        return ServerResponse.createByErrorMessage("查找的体系为空");
    }

    /**
     *  锁定指标
     */
    @PostMapping("/lockSysRelationById")
    @ResponseBody
    public ServerResponse lockSysRelationById(Integer id) {
        if (id != null) {
            int i = relationService.lockSysRelationById(id);
            return ResponseUtils.commonResponse(i, "指标锁定");
        }
        return ServerResponse.createByErrorMessage("锁定的体系的ID为空");
    }
}
